
class Box extends egret.Sprite {
    private labTitle : egret.TextField;
    private imgBackground : egret.Bitmap;
    private imgIcon : egret.Bitmap;
    public row:number;
    public col:number;
    public side:number;
    public level:number;

    static width:number = 0;
    static height:number = 0;
    static intervalX:number = 10;
    static intervalY:number = 10;
    public static offsetX:number = 0;
    public static offsetY:number = 0;

    public static init(){
        var stage = egret.MainContext.instance.stage;
        var stageWidth = stage.stageWidth;
        var stageHeight = stage.stageHeight;
        Box.width = (stageWidth-(Main.boxCol+1)*Box.intervalX)/Main.boxCol;
        Box.height = Box.width+30;
        Box.offsetX = Box.intervalX;
        Box.offsetY = stageHeight-Box.height*Main.boxRow-Box.intervalY*(Main.boxRow+1);
    }

    public constructor(r:number, c:number) {
        super();
        if( Box.width==0 )
            Box.init();

        this.row = r;
        this.col = c;
        this.width = Box.width;
        this.height = Box.height;

        this.graphics.beginFill(0xffffff);
        //this.graphics.drawRect(0, 0, this.width, this.height);
        this.graphics.endFill();

        var imgBackground = new egret.Bitmap();
        this.imgBackground = imgBackground;
        this.imgBackground.width = this.width;
        this.imgBackground.height = this.height;
        this.addChild(imgBackground);

        var imgIcon = new egret.Bitmap();
        this.imgIcon = imgIcon;
        this.imgIcon.anchorX = this.imgIcon.anchorY = 0.5;
        this.imgIcon.x = this.width/2;
        this.imgIcon.y = 90;
        this.addChild(imgIcon);

        var textfield = new egret.TextField();
        textfield.anchorX = 0.5;
        textfield.x = this.width / 2;
        textfield.y = 0;
        textfield.size = 50;
        textfield.text = "2";
        this.labTitle = textfield;
        this.addChild(textfield);

    }

    public perform(game:Main, smode:{}, nmode:{}){
        var tw: egret.Tween;
        if( smode["move"]==-1 )
        {
            tw = egret.Tween.get(this).to({alpha:0}, 80);
            game.lockMove++;
        }
        else if( smode["move"]==1 )
        {
            tw = egret.Tween.get(this).to({x:smode["x"], y:smode["y"]}, 120);
            game.lockMove++;
        }

        if( nmode["new"] == -1 )
        {
            tw.call(function(){
                this.reset();
                egret.Tween.get(this).to({alpha:1}, 150).call(function(){
                    game.lockMove--;
                });
            }, this)
        }
        else if( nmode["new"] == 1 )
        {
            tw.call(function(){
                this.level = nmode["level"];
                this.side = nmode["side"];
                this.alpha = 1;
                this.rebuild();
                game.lockMove--;
            }, this);
        }
    }

    public reset(level:any=null, side:any=null): void {
        this.side = (side!=null)?side:Math.floor(Math.random()*3);
        this.level = (level!=null)?level:Math.pow(2, Math.floor(Math.random()*2)+1);
        //this.level = 2;
        this.rebuild();
        //console.log("reset", this.row, this.col, this.level);
    }

    public rebuild(): void{

        var color = this.side == 0 ? 0xffffff : 0x0;
        this.labTitle.textColor = color;
        this.labTitle.text = this.level.toString();

        var bkres = this.side == 0 ? "bkcn" : "bkjp";
        var texture:egret.Texture = RES.getRes(bkres);
        this.imgBackground.texture = texture;

        var icres = "level"+this.level;
        icres += this.side==0?"c":"j";
        this.imgIcon.texture = RES.getRes(icres);

        this.x = Box.intervalX + this.col * (Box.width+Box.intervalX);
        this.y = Box.offsetY + this.row * (Box.height+Box.intervalY);
    }
}
